package net.lcqf.tcp;

import lombok.extern.slf4j.Slf4j;
import net.lcqf.tcp.utils.LocalCacheUtil;

import java.util.Date;
import java.util.concurrent.*;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/3/6 20:12
 * // +----------------------------------------------------------------------
 */
@Slf4j
public class TcpTestApplication {

    //参赛人数
    private static int count = 5;

    private static CyclicBarrier c;

    private final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
//        for (int i =1;i<=count;i++){
//            String name = "参赛者" + i;
//            TcpTestApplication demo01 = new TcpTestApplication();
//
//            demo01.runingTest(name,i);
//
//        }
        MatchmakingSystem system = new MatchmakingSystem();
        system.start();

        // 模拟玩家加入
        for (int i = 0; i < 20; i++) {
            Player player = new Player();
            player.setId(LocalCacheUtil.ATOMIC.getAndIncrement());
            player.setChannelId(i + "0");
            player.setElo(1500 + i*10);
            player.setJoinTime(System.currentTimeMillis());
            system.addPlayer(player);
        }

    }


    public void runingTest(String name,int num ){
        c = new CyclicBarrier(5);
        executor.execute(new RuningTask(name,num));
    }




    /**
     * 赛跑任务
     */
    class RuningTask implements Runnable{

        private String name;//参赛者姓名
        private int waitTimes;

        RuningTask(String name,int waitTimes){
            this.name = name;
            this.waitTimes = waitTimes;
        }

        @Override
        public void run() {
            try {

                //准备阶段,模拟参赛人员热身准备
                long sleeps = waitTimes*200;
                Thread.sleep(sleeps);
                long st = new Date().getTime();

                log.info("name:" + name + "准备好了,准备了" + sleeps + "毫秒");

                //栅栏位置
                c.await();
//                cycl.await(2, TimeUnit.SECONDS);

                //开跑
                long ed = new Date().getTime();
                long times = ed-st;
                log.info("name:" + name + ",等待了：" + times + "毫秒，开始起跑了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

